﻿using AutoMapper;
using DFGK_WMS.Context;
using DFGK_WMS.IRepository;
using DFGK_WMS.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DFGK_WMS.Repository
{
    public class TReceivingGoodsDetailRepository : BaseRepository<TReceivingGoodsDetail>, ITReceivingGoodsDetailRepository
    {
        public TReceivingGoodsDetailRepository(DFGK_DB context, IMapper mapper) : base(context, mapper)
        {
        }

        public bool AddReceivingGoodsDetail(List<TReceivingGoodsDetail> receivingGoodsDetails)
        {
            //使用事务的原子性
            using (var trans = Context.Database.BeginTransaction())
            {
                try
                {
                    if (receivingGoodsDetails.Count > 0)
                    {
                        foreach (var item in receivingGoodsDetails)
                        {
                            Context.TReceivingGoodsDetails.Add(item);
                            Context.SaveChanges();
                        }
                    }
                    Context.SaveChanges();
                    //提交事务
                    trans.Commit();
                    return true;
                }
                catch (Exception)
                {
                    //事务回滚
                    trans.Rollback();
                    throw;
                }
            }


        }

        public List<TReceivingGoodsDetail> GetReceivingGoodsDetailList(int orderDetailid)
        {
            var res = Context.TReceivingGoodsDetails.Where(r => r.OrderDetailId == orderDetailid).ToList();
            if (res.Count()>0)
            {
                return res;
            }
            return null;
        }

        public int GetUnitIdByUnitName(string unitName)
        {
            var dittct = Context.TDictItems.Where(x => x.ItemName == unitName);
            if (dittct.Count() <= 0)
            {
                return 0;
            }
            return dittct.FirstOrDefault().Id;
        }

        public int GetMaterialCategoryByCode(string code)
        {
            var categoryList = Context.TMaterialCategories.Where(x => x.Code == code);
            if(categoryList.Count() <= 0)
            {
                return 0;
            }
            return categoryList.FirstOrDefault().Id;
        }

        public bool CodeContainZ(string code ,out string msg)
        {
            try
            {
                msg = "";
                var codeV2 = code.Substring(6, 1);
                if (codeV2 != "Z")
                {
                    msg = "器材代码错误";
                    return false;
                }
                if(code.Length < 14)
                {
                    msg = "器材代码错误";
                    return false;
                }
                return true;
            }
            catch
            {
                msg = "器材代码错误";
                return false;
            }

        }

        public bool CodeDoesItExist(string code,string name,out string msg)
        {
            msg = "";
            var materialList = Context.TMaterial.FirstOrDefault(x => x.Code == code);
            if(materialList != null)
            {
                if(materialList.Name != name)
                {
                    msg = "器材代码错误";
                    return false;
                }
            }
            return true;
        }
    }
}
